/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.settings.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.gui.panel.AbstractPanel;
import com.genia.toolbox.basics.editor.model.document.Document;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.document.impl.DocumentModel;

/**
 * The document settings panel.
 */
@SuppressWarnings("serial")
public class DocumentSettingsPanel
    extends AbstractPanel<FormEditorGUI>
    implements DocumentListener
{

  /**
   * The document name label.
   */
  private JLabel labelDocumentName = null;

  /**
   * The document name textfield.
   */
  private JTextField textDocumentName = null;

  /**
   * The document model.
   */
  protected DocumentModel model = null;

  /**
   * Whether the document is a prototype.
   */
  private JCheckBox checkboxIsPrototype = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public DocumentSettingsPanel(FormEditorGUI gui)
  {
    super(gui);

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.setLayout(layout);

    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.add(this.createMainPanel(), constraints);
  }



  /**
   * Create the main panel.
   * 
   * @return the main panel.
   */
  public JPanel createMainPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    panel.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.DOCUMENT_SETTINGS_PANEL_TITLE)));

    this.labelDocumentName = new JLabel(this.getGui().getController().notifyTranslation(Messages.DOCUMENT_SETTINGS_PANEL_LABEL_NAME));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.NORTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    panel.add(this.labelDocumentName, constraints);

    this.textDocumentName = new JTextField();
    this.textDocumentName.getDocument().addDocumentListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTH;
    constraints.insets = new Insets(5, 0, 5, 5);
    panel.add(this.textDocumentName, constraints);

    this.checkboxIsPrototype = new JCheckBox(this.getGui().getController().notifyTranslation(Messages.DOCUMENT_SETTINGS_PANEL_LABEL_ISPROTOTYPE));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.NORTH;
    constraints.insets = new Insets(0, 30, 5, 5);
    panel.add(this.checkboxIsPrototype, constraints);

    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 0, 5, 5);
    panel.add(new JLabel(), constraints);

    return panel;
  }



  /**
   * Set the document.
   * 
   * @param model
   *          the document model.
   */
  public void setDocument(Document<?> model)
  {
    this.textDocumentName.getDocument().removeDocumentListener(this);
    this.model = (DocumentModel) model;
    if (model != null) {
      this.checkboxIsPrototype.setSelected(this.model.isPrototype());
      this.textDocumentName.setText(this.model.getName());
    }
    else {
      this.checkboxIsPrototype.setSelected(false);
      this.textDocumentName.setText("");
    }
    this.textDocumentName.getDocument().addDocumentListener(this);
    this.validateSettings();
  }



  /**
   * Get the document.
   * 
   * @return the document model.
   */
  public DocumentModel getDocument()
  {
    if (this.model != null) {
      this.model.setName(this.textDocumentName.getText());
      this.model.setIsPrototype(this.checkboxIsPrototype.isSelected());
    }
    return this.model;
  }



  /**
   * Handle the document changed.
   * 
   * @param e
   *          the document event.
   */
  public void changedUpdate(DocumentEvent e)
  {
    // Do nothing
  }



  /**
   * Handle the document insert changed.
   * 
   * @param e
   *          the document event.
   */
  public void insertUpdate(DocumentEvent e)
  {
    this.model = this.getDocument();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyDocumentSettingsChanged(this.model);
    this.validateSettings();
  }



  /**
   * Handle the document remove changed.
   * 
   * @param e
   *          the document event.
   */
  public void removeUpdate(DocumentEvent e)
  {
    this.model = this.getDocument();
    this.getGui().notifyDocumentSettingsHasToBeSaved();
    this.getGui().notifyDocumentSettingsChanged(this.model);
    this.validateSettings();
  }



  /**
   * Check whether the settings are valid.
   */
  public void validateSettings()
  {
    if (this.textDocumentName.getText() == null || this.textDocumentName.getText().trim().length() == 0) {
      this.textDocumentName.setForeground(AbstractGUI.COLOR_TEXT_ERROR);
    }
    else {
      this.textDocumentName.setForeground(AbstractGUI.COLOR_TEXT);
    }
  }

}
